package com.fanghuaiming.mtat.sku.controller;

import com.fanghuaiming.mtat.sku.service.MessageListOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/****
 * @description: 订单服务接口
 * @version:1.0.0
 * @author fanghuaiming
 * @data Created in 2020/11/27 下午2:00
 *
 */
@RestController("sku")
@RequestMapping("/sku")
@Slf4j
public class SkuController {

    /**
     * 账户服务
     */
    @Autowired
    private MessageListOrderService messageListOrderService;

    /**
     * @Description: 一次性插入多少条
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/12/4 下午7:21
     * @version:1.0.0
     */
    @GetMapping("/insertSkuByNumber")
    public void insertSkuByNumber(@RequestParam("number") Integer number) {
        try {
            //进行观察
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            messageListOrderService.insertSkuByNumber(number);
            stopWatch.stop();
            log.info("====== 一次性插入{}条数据到SKU表,执行耗时:{}毫秒 ======",number, stopWatch.getTotalTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 单条插入数据库
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/12/4 下午9:55
     * @version:1.0.0
     */
    @GetMapping("/insertSku")
    public void insertSku(@RequestParam("number") Integer number) {
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            messageListOrderService.insertSku(number);
            stopWatch.stop();
            log.info("====== 单条插入{}条数据到SKU表,执行耗时:{}毫秒 ======",number, stopWatch.getTotalTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 批量多线程插入(保证原子性:多线程异步出现异常全部回滚)
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/12/4 下午10:11
     * @version:1.0.0
     */
    @GetMapping("/batchThreadHandle")
    public void batchThreadHandle(@RequestParam("makeException") boolean makeException) {
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            messageListOrderService.batchThreadHandle(makeException);
            stopWatch.stop();
            log.info("batchThreadHandle执行耗时:{}ms", stopWatch.getTotalTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
